﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace AutoRegisterAndActive
{
    public class DeviceHelper
    {
        public static string GetMachineCode()
        {
            //CPU信息
            string cpuInfo = GetMD5Value(GetCpuID() + typeof(string).ToString());
            if (cpuInfo.Equals("UnknowCpuInfo")) return null;
            //磁盘信息
            string diskInfo = GetMD5Value(GetDiskID() + typeof(int).ToString());
            if (diskInfo.Equals("UnknowDiskInfo")) return null;
            //MAC地址
            string macInfo = GetMD5Value(GetMacByNetworkInterface() + typeof(double).ToString());
            if (macInfo.Equals("UnknowMacInfo")) return null;
            //返回机器码
            return GetNum(cpuInfo, 8) + GetNum(diskInfo, 8) + GetNum(macInfo, 8);
        }

        public static string GetMD5Value(string value)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] targetData = md5.ComputeHash(Encoding.Unicode.GetBytes(value));
            string resString = null;
            for (int i = 0; i < targetData.Length; i++)
            {
                resString += targetData[i].ToString("x");
            }
            return resString;
        }

        public static string GetNum(string md5, int len)
        {
            Regex regex = new Regex(@"\d");
            MatchCollection listMatch = regex.Matches(md5);
            string str = "";
            for (int i = 0; i < len; i++)
            {
                str += listMatch[i].Value;
            }
            while (str.Length < len)
            {
                //不足补0
                str += "0";
            }
            return str;
        }



        public static string GetCpuID()
        {
            try
            {
                string cpuInfo = "";
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                moc.Dispose();
                mc.Dispose();
                return cpuInfo;
            }
            catch
            {
                return "UnknowCpuInfo";
            }
        }


        public static string GetDiskID()
        {
            try
            {
                string HDid = "";
                ManagementClass mc = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                }
                moc.Dispose();
                mc.Dispose();
                return HDid;
            }
            catch
            {
                return "UnknowDiskInfo";
            }
        }


        public static string GetMacByNetworkInterface()
        {
            try
            {
                NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface ni in interfaces)
                {
                    return BitConverter.ToString(ni.GetPhysicalAddress().GetAddressBytes());
                }
                return "UnknowMacInfo";
            }
            catch (Exception)
            {
                return "UnknowMacInfo";
            }
        }


    }
}
